//
//  ETitleButtonListView.m
//  Common
//
//  Created by shiqing guo on 2018/7/31.
//  Copyright © 2018年 www.bitkinetic.com. All rights reserved.
//

#import "ETitleButtonListView.h"
#import "Macro.h"
#import "UIView+DDAddition.h"

#define kCollection_Height 20.0f
#define IdentifierID @"ButtonCellId"

@interface ETitleButtonListView () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UILabel *lblTitle;
@property (nonatomic, strong) UICollectionView *clvBtnList;
@property (nonatomic, strong) NSMutableArray *selectBtnArray;
@property (nonatomic, assign) NSInteger selectedIndex;
@end

@implementation ETitleButtonListView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews
{
    [self addSubview:self.lblTitle];
    self.lblTitle.centerY = self.height/2;
    [self addSubview:self.clvBtnList];
    self.clvBtnList.left = self.lblTitle.right;
    self.clvBtnList.centerY = self.height/2;
    self.clvBtnList.width = self.width - self.lblTitle.right;
}

- (void)setStrTitle:(NSString *)strTitle
{
    _strTitle = strTitle;
    self.lblTitle.text = strTitle;
}


- (void)setSelectBtnArray:(NSMutableArray *)selectBtnArray
{
    _selectBtnArray = selectBtnArray;
    [self.clvBtnList reloadData];
}

- (void)setDefaultSelectIndex:(NSInteger)defaultSelectIndex
{
    _defaultSelectIndex = defaultSelectIndex;
    self.selectedIndex = defaultSelectIndex;
    [self.clvBtnList reloadData];
}

#pragma mark - 懒加载
- (UILabel *)lblTitle
{
    if (!_lblTitle)
    {
        _lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80.0f, 22.0f)];
        _lblTitle.textColor = [EPColor epColor_Nav];
        _lblTitle.font = SystemFont(16.0f);
    }
    return _lblTitle;
}

- (UICollectionView *)clvBtnList
{
    if (!_clvBtnList) {
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
        flowlayout.headerReferenceSize = CGSizeMake(0, 0);
        flowlayout.itemSize = CGSizeMake(44, kCollection_Height);  //每个的大小
        flowlayout.minimumLineSpacing = 4.0f;  //每个的纵向间距
        flowlayout.minimumInteritemSpacing = 4.0f;  //每个的横向间距
        _clvBtnList = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 100.0f, kCollection_Height) collectionViewLayout:flowlayout];
        _clvBtnList.delegate = self;
        _clvBtnList.dataSource = self;
        _clvBtnList.showsVerticalScrollIndicator = YES;
        _clvBtnList.showsHorizontalScrollIndicator = YES;
        flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        [_clvBtnList setBackgroundColor:[UIColor clearColor]];
        [_clvBtnList registerClass:[ETitleButtonCell class] forCellWithReuseIdentifier:IdentifierID];
    }
    return _clvBtnList;
}

#pragma mark - collection私有
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.selectBtnArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.selectedIndex = indexPath.row;
    if (self.selectedBlock) self.selectedBlock(self.selectedIndex);
    [self.clvBtnList reloadData];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ETitleButtonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IdentifierID forIndexPath:indexPath];
    
    NSString *strName = [self.selectBtnArray objectAtIndex:indexPath.row];
    if ([strName isKindOfClass:[NSString class]]) {
        cell.strTypeName = strName;
    }
    cell.isSelected = (indexPath.row == self.selectedIndex);
    return cell;
}
@end


